package com.ruoyi.web.controller.bussiness;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.bo.ShiftBo;
import com.ruoyi.system.domain.vo.ShiftVo;
import com.ruoyi.system.service.IShiftService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 班次管理
 *
 * @author ruoyi
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/shift")
public class ShiftController extends BaseController {

    private final IShiftService shiftService;

    /**
     * 查询班次列表
     */
    @GetMapping("/list")
    public TableDataInfo<ShiftVo> list(ShiftBo bo, PageQuery pageQuery) {
        return shiftService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取班次详细信息
     */
    @GetMapping("/{id}")
    public R<ShiftVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(shiftService.queryById(id));
    }

    /**
     * 新增班次
     */
    @Log(title = "班次管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ShiftBo bo) {
        return toAjax(shiftService.insertByBo(bo));
    }

    /**
     * 修改班次
     */
    @Log(title = "班次管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ShiftBo bo) {
        return toAjax(shiftService.updateByBo(bo));
    }

    /**
     * 删除班次
     */
    @Log(title = "班次管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(shiftService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 批量保存班次
     */
    @Log(title = "班次管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/batchSave")
    public R<Void> batchSave(@Validated(AddGroup.class) @RequestBody List<ShiftBo> shifts) {
        return toAjax(shiftService.batchInsertByBo(shifts));
    }
}
